"""日志处理"""
import datetime
import logging
import os
from logging.handlers import TimedRotatingFileHandler
from config import LOGDIR


class ProcessingLog(object):
    # 保存日志的路径和日志名称命名
    log_filename = str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")) + ".log"
    # 日志的设置
    log_name = "WebAutoTest"
    log_level = "DEBUG"
    sh_level = "ERROR"
    fh_level = "INFO"

    @staticmethod
    def create_logger():
        mylog = logging.getLogger(ProcessingLog.log_name)
        mylog.setLevel(ProcessingLog.log_level)
        # 输出渠道（控制台），设置等级
        sh = logging.StreamHandler()
        sh.setLevel(ProcessingLog.sh_level)
        mylog.addHandler(sh)

        # 输出渠道(文件)，设置等级
        fh = TimedRotatingFileHandler(filename=os.path.join(LOGDIR, ProcessingLog.log_filename),
                                      when="D", backupCount=15, encoding="utf-8")

        # fh = logging.FileHandler(filename=os.path.join(ProcessingLog.log_path, ProcessingLog.log_filename),
        #                          encoding="utf-8")

        fh.setLevel(ProcessingLog.fh_level)
        mylog.addHandler(fh)
        # 日志输出格式的设置
        formate = '%(asctime)s - [%(filename)s-->line:%(lineno)d] - %(levelname)s:%(message)s'
        fm = logging.Formatter(formate)
        sh.setFormatter(fm)
        fh.setFormatter(fm)

        return mylog



log = ProcessingLog.create_logger()

